﻿#include <QPainter>
#include "CDevationStateScal.h"
#include "CGlobal.h"

CDevationStateScal::CDevationStateScal(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_dLeft=0.0;//左偏距离单位MM
	m_dRight = 0.0;//右偏距离单位MM
	m_dCorrectSetValue = 0.0;//跑偏判定值(一级)
	m_dCorrectSetValueTwo = 0.0;//跑偏判定值(二级)
	m_nIsHaveBeltState =0;
	m_bBeltState = false;
}

CDevationStateScal::~CDevationStateScal()
{}

void CDevationStateScal::setDeviationValue(double dLeft, double dRight, double dCorrectSetValue, double dCorrectSetValueTwo, int nIsHaveBeltState,bool bBeltState)
{
	m_dLeft = dLeft;
	m_dRight = dRight;
	m_dCorrectSetValue = dCorrectSetValue;
	m_dCorrectSetValueTwo = dCorrectSetValueTwo;
	m_nIsHaveBeltState = nIsHaveBeltState;
	m_bBeltState = bBeltState;
	update();
}

void CDevationStateScal::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	QPen penChu(Qt::black, 2); // 设置画笔颜色为黑色，宽度为2
	QPen penXi(Qt::black, 1); // 设置画笔颜色为黑色，宽度为1
	QPen penXiGreen(Qt::green, 1); // 设置画笔颜色为黑色，宽度为1
	painter.setPen(penXi);
	painter.setFont(QFont(A2T("阿里巴巴普惠体 R"), 12));

	// 获取窗口大小
	int windowWidth = width();
	int windowHeight = height();

	// 绘制大矩形
	painter.setPen(penChu);
	QRect rect(10, 40, windowWidth - 20, 30);
	//painter.drawRect(rect);
	QRect rect1Left(10, 40, 30, 30);//左半圆矩形区域
	QRect rectRight(windowWidth - 40, 40, 30, 30);//右半圆矩形区域
	painter.drawArc(rect1Left, 90 * 16, 180 * 16);
	painter.drawArc(rectRight, 270 * 16, 180 * 16);
	painter.drawLine(25, 40, windowWidth - 25, 40);
	painter.drawLine(25, 70, windowWidth - 25, 70);

	//绘制两条跑偏极限的线
	QPen penXiRed(Qt::red, 1); // 设置画笔颜色为红色，宽度为1
	QPen penXiGray(Qt::gray, 1); // 设置画笔颜色为红色，宽度为1
	QPen penXiBlue(Qt::blue, 1);
	QPen penXiYellow(Qt::yellow, 1);
	painter.setPen(penXiBlue);
	painter.drawLine(40, rect.top()-10, 40, rect.bottom()+10);
	painter.drawLine(windowWidth-50, rect.top() - 10, windowWidth - 50, rect.bottom() + 10);

	int nLeftPos = 40 * m_dLeft / m_dCorrectSetValue+10;
	int nRightPos = 40 * m_dRight / m_dCorrectSetValue+10;

	//绘制水平滑块
	if (m_nIsHaveBeltState == 0)//没有皮带启停信号
	{
		if ((m_dLeft <= m_dCorrectSetValue && m_dLeft > m_dCorrectSetValueTwo) || (m_dRight <= m_dCorrectSetValue && m_dRight > m_dCorrectSetValueTwo))
		{
			painter.setPen(penXiYellow);
			painter.setBrush(Qt::yellow); // 设置填充颜色为黄色
		}
		else if (m_dLeft <= m_dCorrectSetValueTwo || m_dRight <= m_dCorrectSetValueTwo)
		{
			painter.setPen(penXiRed);
			painter.setBrush(Qt::red); // 设置填充颜色为红色
		}
		else
		{
			painter.setPen(penXiGreen);
			painter.setBrush(Qt::green); // 设置填充颜色为绿色
		}
	}
	else
	{
		if (m_bBeltState)
		{
			if ((m_dLeft <= m_dCorrectSetValue && m_dLeft > m_dCorrectSetValueTwo) || (m_dRight <= m_dCorrectSetValue && m_dRight > m_dCorrectSetValueTwo))
			{
				painter.setPen(penXiYellow);
				painter.setBrush(Qt::yellow); // 设置填充颜色为黄色
			}
			else if (m_dLeft <= m_dCorrectSetValueTwo || m_dRight <= m_dCorrectSetValueTwo)
			{
				painter.setPen(penXiRed);
				painter.setBrush(Qt::red); // 设置填充颜色为红色
			}
			else
			{
				painter.setPen(penXiGreen);
				painter.setBrush(Qt::green); // 设置填充颜色为绿色
			}
		}
		else
		{
			painter.setPen(penXiGray);
			painter.setBrush(Qt::gray); // 设置填充颜色为灰色
		}
	}

	QRect rect2(nLeftPos, 40, windowWidth - nLeftPos - nRightPos, 30);
	//painter.drawRect(rect2);
	QRect rect1Left1(nLeftPos, 40, 30, 30);//左半圆矩形区域
	QRect rectRight1(windowWidth - nRightPos - 30, 40, 30, 30);//右半圆矩形区域
	painter.drawPie(rect1Left1, 90 * 16, 180 * 16);
	painter.drawPie(rectRight1, 270 * 16, 180 * 16);
	painter.drawRect(nLeftPos + 15, 40, windowWidth - nLeftPos - nRightPos - 30, 30);
	// 绘制跑偏判定值一级
	painter.setPen(penXi);
	painter.setFont(QFont(A2T("阿里巴巴普惠体 R"), 12));
	painter.drawText(windowWidth / 2 - 100, 100 , A2T("一级跑偏判定值(mm):%1").arg(QString::number(m_dCorrectSetValue,'f',1)));
	painter.drawText(windowWidth / 2 - 100, 120, A2T("二级跑偏判定值(mm):%1").arg(QString::number(m_dCorrectSetValueTwo, 'f', 1)));
}
